/**
 * @author yxw
 * date : 2013年11月22日 下午8:04:10 
 */
package com.tzinfo.tzt.activity.view;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.tzinfo.tzt.R;
import com.tzinfo.tzt.activity.SeeNotesUpdateActivity;
import com.tzinfo.tzt.util.StringTools;
import com.tzinfo.tzt.view.wheel.adapters.ArrayWheelAdapter;
import com.tzinfo.tzt.view.wheel.adapters.NumericWheelAdapter;
import com.tzinfo.tzt.wheel.OnWheelChangedListener;
import com.tzinfo.tzt.wheel.WheelView;

public class MyPickViewPopupWindow extends PopupWindow implements
		OnClickListener {
	private Activity context;
	private SeeNotesUpdateActivity seenotActivity;
	private View mMenuView;
	private Button btn_cancel, btn_ok;
	private TextView contentText;
	private LayoutInflater inflater;
	// 标志
	private int flag = 0;

	// 全局变量
	private int select_year;
	private int select_month;
	private int select_day;
	private int select_week;

	private int select_shi;
	private int select_ting;
	private int select_wei;

	private String[] directions = { "东", "南", "西", "北", "东南", "西南", "西北", "东北" };

	protected WheelView year;
	protected WheelView month;
	protected WheelView day;

	protected WheelView shi;
	protected WheelView ting;
	protected WheelView wei;

	public MyPickViewPopupWindow(Activity context) {
		super(context);
		this.context = context;
		seenotActivity = (SeeNotesUpdateActivity) context;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	// 显示时间滚轮
	public void showTimePick() {
		flag = 0;
		mMenuView = inflater.inflate(R.layout.layout_wheel_time, null);
		btn_cancel = (Button) mMenuView.findViewById(R.id.wheel_cancel);
		btn_cancel.setOnClickListener(this);
		btn_ok = (Button) mMenuView.findViewById(R.id.wheel_ok);
		btn_ok.setOnClickListener(this);
		contentText = (TextView) mMenuView.findViewById(R.id.wheel_content);

		final WheelView hours = (WheelView) mMenuView.findViewById(R.id.hour);
		hours.setViewAdapter(new NumericWheelAdapter(context, 0, 23));
		// hours.setLabel("hours");
		hours.setCyclic(true);

		final WheelView mins = (WheelView) mMenuView.findViewById(R.id.mins);
		mins.setViewAdapter(new NumericWheelAdapter(context, 0, 59, "%02d"));
		// mins.setLabel("mins");
		mins.setCyclic(true);

		String textDate = seenotActivity.text_time.getText().toString();
		if (textDate != null && !"".equals(textDate)) {
			String[] s1 = textDate.split(":");
			contentText.setText(s1[0] + ":"
					+ String.format("%02d", Integer.parseInt(s1[1])));
			hours.setCurrentItem(Integer.parseInt(s1[0]));
			mins.setCurrentItem(Integer.parseInt(s1[1]));
		} else {
			// set current time
			Calendar c = Calendar.getInstance();
			int curHours = c.get(Calendar.HOUR_OF_DAY);
			int curMinutes = c.get(Calendar.MINUTE);
			hours.setCurrentItem(curHours);
			mins.setCurrentItem(curMinutes);
			contentText.setText(curHours + ":"
					+ String.format("%02d", curMinutes));
		}

		mins.addChangingListener(new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// wheel.setLabel(newValue != 1 ? "mins" : "min");
				String[] s = contentText.getText().toString().split(":");
				contentText.setText(s[0] + ":"
						+ String.format("%02d", wheel.getCurrentItem()));
			}
		});
		hours.addChangingListener(new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// wheel.setLabel(newValue != 1 ? "hours" : "hour");
				String[] s = contentText.getText().toString().split(":");
				contentText.setText(wheel.getCurrentItem() + ":" + s[1]);
			}
		});

		this.setContentView(mMenuView);
		this.setWidth(LayoutParams.FILL_PARENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);
		this.setFocusable(true);
		this.setAnimationStyle(R.style.AnimBottom);
		ColorDrawable dw = new ColorDrawable(0x00000000);
		this.setBackgroundDrawable(dw);
		mMenuView.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				int height = mMenuView.findViewById(R.id.wheel_container)
						.getTop();
				int y = (int) event.getY();
				if (event.getAction() == MotionEvent.ACTION_UP) {
					if (y < height) {
						dismiss();
					}
				}
				return true;
			}
		});
	}

	// 显示日期
	public void showDatePick() {
		flag = 1;
		mMenuView = inflater.inflate(R.layout.layout_wheel_date, null);
		btn_cancel = (Button) mMenuView.findViewById(R.id.wheel_cancel);
		btn_cancel.setOnClickListener(this);
		btn_ok = (Button) mMenuView.findViewById(R.id.wheel_ok);
		btn_ok.setOnClickListener(this);
		contentText = (TextView) mMenuView.findViewById(R.id.wheel_content);

		String textDate = seenotActivity.text_date.getText().toString();
		String week;
		if (textDate != null && !"".equals(textDate)) {
			String[] s1 = textDate.split("-");
			select_year = Integer.parseInt(s1[0]) - 1900;
			select_month = Integer.parseInt(s1[1]);
			String[] s2 = s1[2].split(" ");
			select_day = Integer.parseInt(s2[0]);
			week = s2[1];
		} else {
			Calendar c = Calendar.getInstance();
			select_year = c.get(Calendar.YEAR) - 1900;
			select_month = c.get(Calendar.MONTH) + 1;
			select_day = c.get(Calendar.DAY_OF_MONTH);
			select_week = c.get(Calendar.DAY_OF_WEEK);
			week = getWeekCN(select_week);
		}

		year = (WheelView) mMenuView.findViewById(R.id.year);
		year.setViewAdapter(new NumericWheelAdapter(context, 1900, 2100));
		// year.setLabel("年");

		month = (WheelView) mMenuView.findViewById(R.id.month);
		month.setViewAdapter(new NumericWheelAdapter(context, 1, 12));
		// month.setLabel("月");
		month.setCyclic(true);
		day = (WheelView) mMenuView.findViewById(R.id.day);
		day.setViewAdapter(new NumericWheelAdapter(context, 1,
				getMaxDay(select_year + "/" + select_month), "%02d"));
		// day.setLabel("日");
		day.setCyclic(true);

		// set current time
		year.setCurrentItem(select_year);
		month.setCurrentItem(select_month - 1);
		day.setCurrentItem(select_day - 1);

		contentText.setText((select_year + 1900) + "-"
				+ String.format("%02d", select_month) + "-"
				+ String.format("%02d", select_day) + " " + week);

		year.addChangingListener(new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				select_year = wheel.getCurrentItem();
				day.setViewAdapter(new NumericWheelAdapter(context, 1,
						getMaxDay((select_year + 1900) + "/" + select_month),
						"%02d"));
				select_week = getWeekByDate((select_year + 1900) + "/"
						+ select_month + "/" + select_day);
				contentText.setText((select_year + 1900) + "-"
						+ String.format("%02d", select_month) + "-"
						+ String.format("%02d", select_day) + " "
						+ getWeekCN(select_week));
			}
		});
		month.addChangingListener(new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				select_month = wheel.getCurrentItem() + 1;
				day.setViewAdapter(new NumericWheelAdapter(context, 1,
						getMaxDay((select_year + 1900) + "/" + select_month),
						"%02d"));
				select_week = getWeekByDate((select_year + 1900) + "/"
						+ select_month + "/" + select_day);
				contentText.setText((select_year + 1900) + "-"
						+ String.format("%02d", select_month) + "-"
						+ String.format("%02d", select_day) + " "
						+ getWeekCN(select_week));
			}
		});
		day.addChangingListener(new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				select_day = wheel.getCurrentItem() + 1;
				select_week = getWeekByDate((select_year + 1900) + "/"
						+ select_month + "/" + select_day);
				contentText.setText((select_year + 1900) + "-"
						+ String.format("%02d", select_month) + "-"
						+ String.format("%02d", select_day) + " "
						+ getWeekCN(select_week));
			}
		});

		this.setContentView(mMenuView);
		this.setWidth(LayoutParams.FILL_PARENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);
		this.setFocusable(true);
		this.setAnimationStyle(R.style.AnimBottom);
		ColorDrawable dw = new ColorDrawable(0x00000000);
		this.setBackgroundDrawable(dw);
		mMenuView.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				int height = mMenuView.findViewById(R.id.wheel_container)
						.getTop();
				int y = (int) event.getY();
				if (event.getAction() == MotionEvent.ACTION_UP) {
					if (y < height) {
						dismiss();
					}
				}
				return true;
			}
		});
	}

	// 显示朝向滚轮
	public void showDirectionPick() {
		flag = 2;
		mMenuView = inflater.inflate(R.layout.layout_wheel_direction, null);
		btn_cancel = (Button) mMenuView.findViewById(R.id.wheel_cancel);
		btn_cancel.setOnClickListener(this);
		btn_ok = (Button) mMenuView.findViewById(R.id.wheel_ok);
		btn_ok.setOnClickListener(this);
		contentText = (TextView) mMenuView.findViewById(R.id.wheel_content);

		final WheelView direction = (WheelView) mMenuView
				.findViewById(R.id.direction);
		direction.setViewAdapter(new ArrayWheelAdapter(context, directions));
		// hours.setLabel("hours");

		String textDate = seenotActivity.text_direction.getText().toString();
		if (textDate != null && !"".equals(textDate)) {
			int i = 0;
			for (i = 0; i < directions.length; i++) {
				if (directions[i].equals(textDate))
					break;
			}
			String s1 = directions[i];
			contentText.setText(s1);
			direction.setCurrentItem(i);
		} else {
			// set current time
			contentText.setText(directions[0]);
			direction.setCurrentItem(0);
		}

		direction.addChangingListener(new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// wheel.setLabel(newValue != 1 ? "mins" : "min");
				contentText.setText(directions[wheel.getCurrentItem()]);
			}
		});

		this.setContentView(mMenuView);
		this.setWidth(LayoutParams.FILL_PARENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);
		this.setFocusable(true);
		this.setAnimationStyle(R.style.AnimBottom);
		ColorDrawable dw = new ColorDrawable(0x00000000);
		this.setBackgroundDrawable(dw);
		mMenuView.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				int height = mMenuView.findViewById(R.id.wheel_container)
						.getTop();
				int y = (int) event.getY();
				if (event.getAction() == MotionEvent.ACTION_UP) {
					if (y < height) {
						dismiss();
					}
				}
				return true;
			}
		});
	}

	// 显示楼层滚轮
	public void showFloorPick() {
		flag = 3;
		mMenuView = inflater.inflate(R.layout.layout_wheel_time, null);
		btn_cancel = (Button) mMenuView.findViewById(R.id.wheel_cancel);
		btn_cancel.setOnClickListener(this);
		btn_ok = (Button) mMenuView.findViewById(R.id.wheel_ok);
		btn_ok.setOnClickListener(this);
		contentText = (TextView) mMenuView.findViewById(R.id.wheel_content);

		final WheelView floor1 = (WheelView) mMenuView.findViewById(R.id.hour);
		floor1.setViewAdapter(new NumericWheelAdapter(context, 1, 99));
		// hours.setLabel("hours");

		final WheelView floor2 = (WheelView) mMenuView.findViewById(R.id.mins);
		floor2.setViewAdapter(new NumericWheelAdapter(context, 1, 99));
		// mins.setLabel("mins");

		String textDate = seenotActivity.text_floor.getText().toString();
		if (textDate != null && !"".equals(textDate)) {
			String[] s1 = textDate.split("/");
			contentText.setText(s1[0] + "/" + s1[1]);
			floor1.setCurrentItem(Integer.parseInt(s1[0]));
			floor2.setCurrentItem(Integer.parseInt(s1[1]));
		} else {
			// set current time
			floor1.setCurrentItem(0);
			floor2.setCurrentItem(0);
			contentText.setText(1 + "/" + 1);
		}

		floor1.addChangingListener(new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// wheel.setLabel(newValue != 1 ? "mins" : "min");
				String[] s = contentText.getText().toString().split("/");
				if (wheel.getCurrentItem() > Integer.parseInt(s[1]) - 1) {
					s[0] = s[1];
					wheel.setCurrentItem(Integer.parseInt(s[1]) - 1);
				}
				contentText.setText((wheel.getCurrentItem() + 1) + "/" + s[1]);
			}
		});
		floor2.addChangingListener(new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// wheel.setLabel(newValue != 1 ? "hours" : "hour");
				String[] s = contentText.getText().toString().split("/");
				if (wheel.getCurrentItem() < Integer.parseInt(s[0]) - 1) {
					s[0] = (wheel.getCurrentItem() + 1) + "";
					floor1.setCurrentItem(Integer.parseInt(s[0]) - 1);
				}
				contentText.setText(s[0] + "/" + (wheel.getCurrentItem() + 1));
			}
		});

		this.setContentView(mMenuView);
		this.setWidth(LayoutParams.FILL_PARENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);
		this.setFocusable(true);
		this.setAnimationStyle(R.style.AnimBottom);
		ColorDrawable dw = new ColorDrawable(0x00000000);
		this.setBackgroundDrawable(dw);
		mMenuView.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				int height = mMenuView.findViewById(R.id.wheel_container)
						.getTop();
				int y = (int) event.getY();
				if (event.getAction() == MotionEvent.ACTION_UP) {
					if (y < height) {
						dismiss();
					}
				}
				return true;
			}
		});
	}


	// 显示日期
	public void showTypePick() {
		flag = 4;
		mMenuView = inflater.inflate(R.layout.layout_wheel_type, null);
		btn_cancel = (Button) mMenuView.findViewById(R.id.wheel_cancel);
		btn_cancel.setOnClickListener(this);
		btn_ok = (Button) mMenuView.findViewById(R.id.wheel_ok);
		btn_ok.setOnClickListener(this);
		contentText = (TextView) mMenuView.findViewById(R.id.wheel_content);

		String textDate = seenotActivity.text_type.getText().toString();
		if (textDate != null && !"".equals(textDate)) {
			List<Integer> list = StringTools.getNumbers(textDate);
			select_shi = list.get(0);
			select_ting = list.get(1);
			select_wei = list.get(2);
		} else {
			select_shi = 0;
			select_ting = 0;
			select_wei = 0;
		}

		shi = (WheelView) mMenuView.findViewById(R.id.shi);
		shi.setViewAdapter(new NumericWheelAdapter(context, 0, 10));

		ting = (WheelView) mMenuView.findViewById(R.id.ting);
		ting.setViewAdapter(new NumericWheelAdapter(context, 0, 10));
		wei = (WheelView) mMenuView.findViewById(R.id.wei);
		wei.setViewAdapter(new NumericWheelAdapter(context, 0, 10));

		// set current item
		shi.setCurrentItem(select_shi);
		ting.setCurrentItem(select_ting);
		wei.setCurrentItem(select_wei);

		contentText.setText(select_shi + "室" + select_ting + "厅" + select_wei
				+ "卫");

		shi.addChangingListener(new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				select_shi = wheel.getCurrentItem();
				contentText.setText(select_shi + "室" + select_ting + "厅" + select_wei
						+ "卫");
			}
		});
		ting.addChangingListener(new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				select_ting = wheel.getCurrentItem();
				contentText.setText(select_shi + "室" + select_ting + "厅" + select_wei
						+ "卫");
			}
		});
		wei.addChangingListener(new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				select_wei = wheel.getCurrentItem();
				contentText.setText(select_shi + "室" + select_ting + "厅" + select_wei
						+ "卫");
			}
		});

		this.setContentView(mMenuView);
		this.setWidth(LayoutParams.FILL_PARENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);
		this.setFocusable(true);
		this.setAnimationStyle(R.style.AnimBottom);
		ColorDrawable dw = new ColorDrawable(0x00000000);
		this.setBackgroundDrawable(dw);
		mMenuView.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				int height = mMenuView.findViewById(R.id.wheel_container)
						.getTop();
				int y = (int) event.getY();
				if (event.getAction() == MotionEvent.ACTION_UP) {
					if (y < height) {
						dismiss();
					}
				}
				return true;
			}
		});
	}

	protected String getWeekCN(int week) {
		String sWeekDay = "周一";
		switch (week) {
		case Calendar.MONDAY:
			sWeekDay = "周一";
			break;
		case Calendar.TUESDAY:
			sWeekDay = "周二";
			break;
		case Calendar.WEDNESDAY:
			sWeekDay = "周三";
			break;
		case Calendar.THURSDAY:
			sWeekDay = "周四";
			break;
		case Calendar.FRIDAY:
			sWeekDay = "周五";
			break;
		case Calendar.SATURDAY:
			sWeekDay = "周六";
			break;
		case Calendar.SUNDAY:
			sWeekDay = "周日";
			break;
		}
		return sWeekDay;
	}

	// 得到星期
	public int getWeekByDate(String year_month_day) {
		Calendar rightNow = Calendar.getInstance();
		SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy/MM/dd");
		try {
			rightNow.setTime(simpleDate.parse(year_month_day)); // 要计算你想要的月份，改变这里即可
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int weeks = rightNow.get(Calendar.DAY_OF_WEEK);
		return weeks;
	}

	// 得到当月最大天数
	public int getMaxDay(String year_month) {
		Calendar rightNow = Calendar.getInstance();
		SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy/MM");
		try {
			rightNow.setTime(simpleDate.parse(year_month)); // 要计算你想要的月份，改变这里即可
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int days = rightNow.getActualMaximum(Calendar.DAY_OF_MONTH);
		return days;
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.wheel_cancel:
			dismiss();
			break;
		case R.id.wheel_ok:
			switch (flag) {
			case 0:
				seenotActivity.text_time.setText(contentText.getText()
						.toString());
				break;
			case 1:
				seenotActivity.text_date.setText(contentText.getText()
						.toString());
				break;
			case 2:
				seenotActivity.text_direction.setText(contentText.getText()
						.toString());
				break;
			case 3:
				seenotActivity.text_floor.setText(contentText.getText()
						.toString());
				break;
			case 4:
				seenotActivity.text_type.setText(contentText.getText()
						.toString());
				break;

			default:
				break;
			}
			dismiss();
			break;
		}
	}
}